package phoenix.mob_android.page_objects;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.openqa.selenium.WebElement;

import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.PageBaseMobAndroidExt;
import phoenix.core.config.extensions.ConfigExt;
import phoenix.mob_android.locators.base.ChatPageLocatorsBase;

public class ChatPage extends PageBaseMobAndroidExt
{
	/**
	 * Initializes the ChatPage
	 * @param p_manager
	 *            PhoenixManager instance
	 */
	public ChatPage(PhoenixManager p_manager)
	{
		super(p_manager);
	}

	/**
	 * Gets web elements by site
	 * @return IndexPageLocatorsBase instance
	 */
	public ChatPageLocatorsBase elements()
	{
		switch (manager.testModel().getSiteName())
		{
			case ConfigExt.SITE_ID_FLIRT:
			{
				return new phoenix.mob_android.locators.flirt.ChatPageLocators();
			}
			case ConfigExt.SITE_ID_WILDBUDDIES:
			{
				return new phoenix.mob_android.locators.wildbuddies.ChatPageLocators();
			}
			case ConfigExt.SITE_ID_CHEECKYLOVERS:
			{
				return new phoenix.mob_android.locators.cheeckylovers.ChatPageLocators();
			}
			default:
			{
				return null;
			}
		}
	}

	public void clickBackAndroidButton()
	{
		manager.md().driver().navigate().back();
	}

	public ChatPage isOnChatPage()
	{
		md().waitForPresence(elements().blockChatPage, "Chat page doesn't appear");

		return this;
	}

	public ChatPage chooseChatRoom(int p_chatNum)
	{
		md().waitForVisibility(elements().selectChat, "List of chat rooms doesn't appear");
		WebElement chatRoom = manager.wd().driver().findElements(elements().selectChat).get(p_chatNum);
		md().wait(2);
		chatRoom.click();

		return this;
	}

	public ChatPage openChatrooms()
	{
		md().waitForPresence(elements().btnChatRooms, "Chat rooms button doesn't appear");
		md().click(elements().btnChatRooms);

		return this;
	}

	public void sendTwoMessagesToPrivateChat(String p_message)
	{
		md().waitForVisibility(elements().inputFieldMessage, "Input field for message doesn't appear");
		md().typeAndClear(elements().inputFieldMessage, p_message);
		md().click(elements().btnSendMessage);
		md().waitForPresence(elements().blockMyMessage, "My message block doesn't appear");
		md().typeAndClear(elements().inputFieldMessage, p_message);
		md().click(elements().btnSendMessage);
	}

	public void checkForRightMenuOpened()
	{
		md().waitForVisibility(elements().blockRightMenu, "Right menu block doesn't open");
		md().waitForVisibility(elements().btnFilterGroup, "Group filter buttons doesn't appear");
	}

	public ChatPage openRightMenu()
	{
		md().wait(5);
		md().waitForVisibility(elements().btnRightMenu, "Right menu button doesn't appear");
		WebElement btn_right_menu = md().getWebElement(elements().btnRightMenu);
		md().tap(1, btn_right_menu, 1);
		md().wait(3);
		if (!md().isElementDisplayed(elements().blockRightMenu))
		{
			md().click(elements().btnRightMenu);
		}

		return this;
	}

	public Map<Integer, String[]> sendMessageToChatRoom(String tmpMessage)
	{
		md().waitForPresence(elements().blockChatPage, "Block chat page doesn't appear");
		md().waitForPresence(elements().inputFieldMessage, "Input field for message doesn't appear");
		md().type(elements().inputFieldMessage, tmpMessage);
		md().wait(3);
		md().waitForVisibility(elements().btnSendMessage, "Button send message doesn't appear");
		md().click(elements().btnSendMessage);
		md().waitForPresence(elements().blockMyMessage, "My message block doesn't appear");
		Map<Integer, String[]> p_blockMessageResults = new HashMap<Integer, String[]>();
		List<WebElement> block_my_message = md().getWebElements(elements().blockMyMessage);
		Integer p_id = 0;
		for (WebElement elementProfile : block_my_message)
		{
			String p_author = elementProfile.findElement(elements().blockMyEmail).getText();
			String p_message = elementProfile.findElement(elements().blockTextMessage).getText();
			p_blockMessageResults.put(p_id, new String[] { p_author, p_message });
			p_id++;
		}

		return p_blockMessageResults;
	}

	public void isOnPrivateChatPage()
	{
		md().waitForVisibility(elements().blockChatPage, "Chat page doesn't appear");
		md().waitForPresence(elements().inputFieldMessage, "Input field for message doesn't appear");
	}

	public void clickUpgradeButton()
	{
		md().waitForPresence(elements().btnUpgrade, "Button upgrade doesn't appear on the chat page");
		md().click(elements().btnUpgrade);
	}
}
